﻿using System;
using Avalonia.Controls;
using iOSClub.App.Share.WeatherModels;

namespace iOSClub.App.Desktop.Dialogs;

public partial class SunMoonLook : UserControl
{
    public SunMoonLook()
    {
        InitializeComponent();
    }

    private void SunProgressInitialized(object? sender, EventArgs e)
    {
        if(sender is not ProgressBar bar)return;
        if(DataContext is not WeatherToday today)return;
        bar.Value = DateProgress(today.sunrise,today.sunset);
    }
    
    private int DateProgress(string start, string end)
    {
        if (string.IsNullOrEmpty(start) || string.IsNullOrEmpty(end)) return 100;

        var now = DateTime.Now;
        var startDate = DateTime.Parse(start);
        var endDate = DateTime.Parse(end);
        var total = endDate - startDate;
        var nowProgress = now - startDate;
        var totalMin = total.TotalMinutes;
        if (totalMin < 0)
            totalMin = 24 * 60 - totalMin;
        var result = (int)(nowProgress.TotalMinutes / totalMin * 100);
        return result > 100 ? 100 : result;
    }

    private void MoonProgressInitialized(object? sender, EventArgs e)
    {
        if(sender is not ProgressBar bar)return;
        if(DataContext is not WeatherToday today)return;
        bar.Value = DateProgress(today.moonrise,today.moonSet);
    }
}